//
//  IndebtRecordVC.swift
//  物料商行
//
//  Created by HaoDong Hong on 2017/9/27.
//  Copyright © 2017年 HaoDong Hong. All rights reserved.
//

import UIKit
import SVProgressHUD

class IndebtRecordVC: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!
    var items = [IndebtRecord]()
    let helper = MaterialHelper()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0)
        tableView.register(UINib(nibName: "IndebtRecordCell", bundle: nil), forCellReuseIdentifier: "IndebtRecordCell")
        requestRecords()
        
    }
    
    func requestRecords() {
        SVProgressHUD.show()
        helper.getIndebt { (error, records) in
            if error != nil {
                SVProgressHUD.showError(withStatus: error.debugDescription)
            } else {
                self.items = records!
                self.tableView.reloadData()
            }
            SVProgressHUD.dismiss(withDelay: 1.0)
        }
    }

}

extension IndebtRecordVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "IndebtRecordCell") as! IndebtRecordCell
        let item = items[indexPath.row]
        cell.dateLabel.text = item.createAt
        if let url = URL(string: item.logo) {
            cell.logoImage.kf.setImage(with: url)
        }
        cell.storeNameLabel.text = item.storeName
        cell.priceLabel.text = item.limit
        cell.qixianLabel.text = item.date
        cell.beizhuLabel.text = ""
        return cell
    }
}
